if {"::tcltest" ni [namespace children]} {
	package require tcltest
	namespace import ::tcltest::*
}

package require rl_json
package require parse_args
namespace path {::rl_json ::parse_args}

source [file join [file dirname [info script]] helpers.tcl]

test unset-1.1 {Unset a key in an object} -setup { #<<<
	set json {
		{
			"foo": "Foo",
			"bar": "Bar",
			"baz": "Baz"
		}
	}
} -body {
	json unset json bar
	compare_json {
		{
			"foo": "Foo",
			"baz": "Baz"
		}
	} $json
} -cleanup {
	unset -nocomplain json
} -result match
#>>>
test unset-1.2 {No args} -body { #<<<
	set json	"{}"
	json unset json
} -cleanup {
	unset -nocomplain json r o
} -result "{}"
#>>>
test unset-1.3 {No args, invalid varname} -setup { #<<<
	unset -nocomplain notset
} -body {
	list [catch {json unset notset} r o] [dict get $o -errorcode]
} -cleanup {
	unset -nocomplain r o
} -result {1 {TCL LOOKUP VARNAME notset}}
#>>>
test unset-2.1 {Do Nothing Gracefully (sort of - don't blow up if an empty path is supplied} -setup { #<<<
	set json {
		{
			"foo": "Foo",
			"baz": "Baz"
		}
	}
} -body {
	json unset json
	compare_json {
		{
			"foo": "Foo",
			"baz": "Baz"
		}
	} $json
} -cleanup {
	unset -nocomplain json
} -result match
#>>>
test unset-2.2 {Return value of unset is the new variable's value} -setup { #<<<
	set json {
		{
			"foo": "Foo",
			"bar": "Bar",
			"baz": "Baz"
		}
	}
} -body {
	json unset json bar
} -cleanup {
	unset -nocomplain json
} -result {{"foo":"Foo","baz":"Baz"}}
#>>>
test unset-3.1 {Throw error for missing objects in path} -setup { #<<<
	set json {
		{
			"foo": "Foo",
			"baz": "Baz"
		}
	}
} -body {
	list [catch {json unset json bar Bar} r o] $r [dict get $o -errorcode]
} -cleanup {
	unset -nocomplain json r o
} -result {1 {Path element "bar" doesn't exist} {RL JSON BAD_PATH bar}}
#>>>
test unset-3.1 {Throw error for dereferencing atomic objects in path} -setup { #<<<
	set json {
		{
			"foo": "Foo",
			"baz": "Baz"
		}
	}
} -body {
	list [catch {json unset json foo Bar} r o] [dict get $o -errorcode]
} -cleanup {
	unset -nocomplain json r o
} -result {1 {RL JSON BAD_PATH {foo Bar}}}
#>>>
test unset-4.1 {Named variable doesn't exist} -body { #<<<
	list [catch {json unset newvar x} r o] [dict get $o -errorcode]
} -cleanup {
	unset -nocomplain newvar r o
} -result {1 {TCL LOOKUP VARNAME newvar}}
#>>>
test unset-5.1 {Unset an existing element in an array, numbered} -setup { #<<<
	set json {
		{
			"foo": ["a", "b", "c"]
		}
	}
} -body {
	json unset json foo 1
	set json
} -cleanup {
	unset -nocomplain json
} -result {{"foo":["a","c"]}}
#>>>
test unset-5.2 {Unset an element in an array, negative index} -setup { #<<<
	set json {
		{
			"foo": ["a", "b", "c"]
		}
	}
} -body {
	json unset json foo -1
	set json
} -cleanup {
	unset -nocomplain json
} -result {{"foo":["a","b","c"]}}
#>>>
test unset-5.3 {Unset an element in an array, numbered, just beyond end} -setup { #<<<
	set json {
		{
			"foo": ["a", "b", "c"]
		}
	}
} -body {
	json unset json foo 3
	set json
} -cleanup {
	unset -nocomplain json
} -result {{"foo":["a","b","c"]}}
#>>>
test unset-5.4 {Unset an element in an array, numbered, 2 beyond end} -setup { #<<<
	set json {
		{
			"foo": ["a", "b", "c"]
		}
	}
} -body {
	json unset json foo 5
	set json
} -cleanup {
	unset -nocomplain json
} -result {{"foo":["a","b","c"]}}
#>>>
test unset-6.1 {Unset an existing element in an array, end} -setup { #<<<
	set json {
		{
			"foo": ["a", "b", "c"]
		}
	}
} -body {
	json unset json foo end
	set json
} -cleanup {
	unset -nocomplain json
} -result {{"foo":["a","b"]}}
#>>>
test unset-6.2 {Unset an element in an array, end-relative} -setup { #<<<
	set json {
		{
			"foo": ["a", "b", "c"]
		}
	}
} -body {
	json unset json foo end-1
	set json
} -cleanup {
	unset -nocomplain json
} -result {{"foo":["a","c"]}}
#>>>
test unset-6.3 {Unset an element in an array, end-relative, just beyond end} -setup { #<<<
	set json {
		{
			"foo": ["a", "b", "c"]
		}
	}
} -body {
	json unset json foo end+1
	set json
} -cleanup {
	unset -nocomplain json
} -result {{"foo":["a","b","c"]}}
#>>>
test unset-6.4 {Unset an element in an array, end-relative, 2 beyond end} -setup { #<<<
	set json {
		{
			"foo": ["a", "b", "c"]
		}
	}
} -body {
	json unset json foo end+3
	set json
} -cleanup {
	unset -nocomplain json
} -result {{"foo":["a","b","c"]}}
#>>>
test unset-7.1 {Unset an element in an array, end-relative} -setup { #<<<
	set json {
		{
			"foo": ["a", ["1","2","3"], "c"]
		}
	}
} -body {
	json unset json foo end-1 1
	set json
} -cleanup {
	unset -nocomplain json
} -result {{"foo":["a",["1","3"],"c"]}}
#>>>
test unset-7.2 {Unset an element in an array, end-relative, invalid} -setup { #<<<
	set json {
		{
			"foo": ["a", ["1","2","3"], "c"]
		}
	}
} -body {
	json unset json foo end/1 1
	set json
} -cleanup {
	unset -nocomplain json
} -returnCodes error -result {Expected an integer index or end(+/-integer)?, got end/1}
#>>>
test unset-7.3 {Unset an element in an array, end-relative, invalid} -setup { #<<<
	set json {
		{
			"foo": ["a", ["1","2","3"], "c"]
		}
	}
} -body {
	json unset json foo en 1
	set json
} -cleanup {
	unset -nocomplain json
} -returnCodes error -result {Expected an integer index or end(+/-integer)?, got en}
#>>>
test unset-7.4 {Unset an element in an array, end-relative, invalid} -setup { #<<<
	set json {
		{
			"foo": ["a", ["1","2","3"], "c"]
		}
	}
} -body {
	json unset json foo end-1x 1
	set json
} -cleanup {
	unset -nocomplain json
} -returnCodes error -result {Expected an integer index or end(+/-integer)?, got end-1x}
#>>>
test unset-7.5 {Unset an element in an array, invalid} -setup { #<<<
	set json {
		{
			"foo": ["a", ["1","2","3"], "c"]
		}
	}
} -body {
	json unset json foo -1 1
	set json
} -cleanup {
	unset -nocomplain json
} -returnCodes error -result {Path element "foo -1" doesn't exist}
#>>>
test unset-7.6 {Unset an element in an array, invalid} -setup { #<<<
	set json {
		{
			"foo": ["a", ["1","2","3"], "c"]
		}
	}
} -body {
	json unset json foo 4 1
	set json
} -cleanup {
	unset -nocomplain json
} -returnCodes error -result {Path element "foo 4" doesn't exist}
#>>>
test unset-7.7 {Unset an element in an array, invalid} -setup { #<<<
	set json {
		{
			"foo": ["~S:a", ["1","2","3"], "c"]
		}
	}
} -body {
	json unset json foo 0 1
	set json
} -cleanup {
	unset -nocomplain json
} -returnCodes error -result {Attempt to index into atomic type string at path "foo 0 1"}
#>>>
test unset-7.8 {Unset an element in an array, invalid} -setup { #<<<
	set json {
		{
			"foo": ["~S:a", ["1","2","3"], "c"]
		}
	}
} -body {
	json unset json foo 1 end/1
	set json
} -cleanup {
	unset -nocomplain json
} -returnCodes error -result {Expected an integer index or end(+/-integer)?, got end/1}
#>>>
test unset-7.9 {Unset an element in an array, invalid} -setup { #<<<
	set json {
		{
			"foo": ["~S:a", ["1","2","3"], "c"]
		}
	}
} -body {
	json unset json foo 1 en
	set json
} -cleanup {
	unset -nocomplain json
} -returnCodes error -result {Expected an integer index or end(+/-integer)?, got en}
#>>>
test unset-7.10 {Unset an element in an array, invalid} -setup { #<<<
	set json {
		{
			"foo": ["~S:a", ["1","2","3"], "c"]
		}
	}
} -body {
	json unset json foo 1 end-1x
	set json
} -cleanup {
	unset -nocomplain json
} -returnCodes error -result {Expected an integer index or end(+/-integer)?, got end-1x}
#>>>

::tcltest::cleanupTests
return

# vim: ft=tcl foldmethod=marker foldmarker=<<<,>>> ts=4 shiftwidth=4
